﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;

namespace ApqDBManager
{
	public partial class MainForm : Apq.Windows.Forms.ImeForm
	{
		public MainForm()
		{
			InitializeComponent();
		}

		public override void SetUILang(Apq.UILang.UILang UILang)
		{
			#region 菜单
			tsmiFile.Text = Apq.GlobalObject.UILang["主菜单(&M)"];
			tsmiNew.Text = Apq.GlobalObject.UILang["新建(&N)"];
			tsmiOpen.Text = Apq.GlobalObject.UILang["打开(&O)"];
			tsmiHistory.Text = Apq.GlobalObject.UILang["历史(&H)"];
			tsmiSave.Text = Apq.GlobalObject.UILang["保存(&S)"];
			tsmiSaveAs.Text = Apq.GlobalObject.UILang["另存为(&A)"];
			tsmiExit.Text = Apq.GlobalObject.UILang["退出(&X)"];

			tsmiTool.Text = Apq.GlobalObject.UILang["工具(&T)"];
			tsmiOption.Text = Apq.GlobalObject.UILang["选项(&O)"];
			tsmiUILang.Text = Apq.GlobalObject.UILang["语言(&L)"];
			tsmiRandom.Text = Apq.GlobalObject.UILang["随机串生成器(&R)"];

			tsmiWindow.Text = Apq.GlobalObject.UILang["窗口(&W)"];
			tsmiCloseAll.Text = Apq.GlobalObject.UILang["全部关闭(&L)"];

			tsmiHelp.Text = Apq.GlobalObject.UILang["帮助(&H)"];
			tsmiSN.Text = Apq.GlobalObject.UILang["注册信息(&R)"];
			tsmiAbout.Text = Apq.GlobalObject.UILang["关于(&A)"];
			#endregion

			this.tsmiNew.Image = System.Drawing.Image.FromFile(Application.StartupPath + @"\Res\png\File\New.png");
			this.tsmiOpen.Image = System.Drawing.Image.FromFile(Application.StartupPath + @"\Res\png\File\Open.png");
			this.tsmiSave.Image = System.Drawing.Image.FromFile(Application.StartupPath + @"\Res\png\File\Save.png");

			dsHistory.dtExplorer.DefaultView.RowFilter = "FileName LIKE '%sql'";
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			#region 读取标题
			string cfgText = GlobalObject.XmlConfigChain[this.GetType(), "Text"];
			if (cfgText != null)
			{
				Text = cfgText;
			}
			#endregion

			tsmiNew_Click(sender, e);
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		#region 文件
		//新建
		private void tsmiNew_Click(object sender, EventArgs e)
		{
			Apq.Windows.Forms.DockFrameForm win = new Apq.Windows.Forms.DockFrameForm();
			win.DockDoc.DockLeftPortion = 300;
			win.DockDoc.DockRightPortion = 300;
			win.DockDoc.DockTopPortion = 350;
			win.DockDoc.DockBottomPortion = 400;
			win.DocForm = new Forms.SqlEdit();
			win.Show(dockPanel1);
		}
		//退出
		private void tsmiExit_Click(object sender, EventArgs e)
		{
			Close();
		}
		#endregion

		#region 工具
		private void tsmiOption_Click(object sender, EventArgs e)
		{
			Forms.MainOption win = new Forms.MainOption();
			win.ShowDialog(this);
		}

		private void tsmiUILang_Click(object sender, EventArgs e)
		{
			DockContent dc = (DockContent)Apq.Windows.Forms.SingletonForms.GetInstance(typeof(Apq.Windows.Forms.DockForms.UILangCfg));
			dc.Show(dockPanel1);
		}

		private void tsmiRandom_Click(object sender, EventArgs e)
		{
			Forms.Random win = new Forms.Random();
			win.Show(dockPanel1);
		}
		#endregion

		#region 窗口
		private void tsmiCloseAll_Click(object sender, EventArgs e)
		{
			foreach (Form childForm in MdiChildren)
			{
				childForm.Close();
			}
		}

		private void tsmiNewApp_Click(object sender, EventArgs e)
		{
			System.Diagnostics.Process.Start(Application.ExecutablePath);
		}
		#endregion

		#region 帮助
		private void tsmiSN_Click(object sender, EventArgs e)
		{
			Form f = new Apq.Reg.Client.Win.RegForm();
			f.ShowDialog(this);
		}

		private void tsmiAbout_Click(object sender, EventArgs e)
		{
			GlobalObject.AboutBox.Asm = GlobalObject.TheAssembly;
			GlobalObject.AboutBox.ShowDialog(this);
		}
		#endregion

		private Apq.XSD.Explorer dsHistory = new Apq.XSD.Explorer();
		private long NextID = 1;

		//历史
		private void tsmiHistory_MouseEnter(object sender, EventArgs e)
		{
			tsmiHistory.DropDownItems.Clear();

			string DirFullPath = GlobalObject.XmlConfigChain[typeof(MainForm), "HFolder"];
			if (!System.IO.Directory.Exists(DirFullPath))
			{
				System.IO.Directory.CreateDirectory(DirFullPath);
			}

			dsHistory.dtExplorer.Clear();
			NextID = 1;
			Apq.IO.FileSystem.LoadFiles(dsHistory.dtExplorer, ref NextID, 0, 0, DirFullPath);
			//超过150个文件时只保留前100个文件
			if (dsHistory.dtExplorer.Rows.Count > 150)
			{
				for (int i = dsHistory.dtExplorer.Rows.Count - 1; i >= 100; i--)
				{
					Apq.XSD.Explorer.dtExplorerRow dr = dsHistory.dtExplorer[i];
					try
					{
						File.Delete(dr.FullName);
						dsHistory.dtExplorer.Rows.RemoveAt(i);
					}
					catch { }
				}
			}

			//读取历史文件夹并将前20个文件名作为菜单倒序加到子项
			for (int i = 0; i < 20 && i < dsHistory.dtExplorer.Rows.Count; i++)
			{
				Apq.XSD.Explorer.dtExplorerRow dr = dsHistory.dtExplorer[i];
				string FileName1 = System.IO.Path.GetFileNameWithoutExtension(dr.FileName);
				ToolStripMenuItem tsmi = new ToolStripMenuItem(FileName1);
				tsmi.Size = new System.Drawing.Size(201, 22);
				tsmi.Text = FileName1;
				tsmi.Click += new System.EventHandler(tsmiHC_Click);

				tsmiHistory.DropDownItems.Insert(0, tsmi);
			}
		}

		private void tsmiHC_Click(object sender, EventArgs e)
		{
			ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
			if (tsmi != null)
			{
				Apq.Windows.Forms.DockFrameForm FormEditor = ActiveMdiChild as Apq.Windows.Forms.DockFrameForm;
				if (FormEditor != null)
				{
					ApqDBManager.Forms.SqlEdit Editor = FormEditor.DocForm as ApqDBManager.Forms.SqlEdit;
					if (Editor != null)
					{
						string DirFullPath = GlobalObject.XmlConfigChain[typeof(MainForm), "HFolder"];
						if (!System.IO.Directory.Exists(DirFullPath))
						{
							System.IO.Directory.CreateDirectory(DirFullPath);
						}
						Editor.LoadFile(DirFullPath + "\\" + tsmi.Text + ".sql");
					}
				}
			}
		}
	}
}